package com.net.shop.car.manager.ui.personalcenter.dingdan;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.view.View;
import android.view.ViewGroup;
import android.widget.ExpandableListView;
import android.widget.ExpandableListView.OnGroupClickListener;
import android.widget.ExpandableListView.OnGroupExpandListener;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.net.shop.car.manager.App;
import com.net.shop.car.manager.BaseActivity;
import com.net.shop.car.manager.R;
import com.net.shop.car.manager.api.model.Dingdan;
import com.net.shop.car.manager.api.model.DingdanDetail;
import com.net.shop.car.manager.api.model.Member;
import com.net.shop.car.manager.api.model.OilCardDingDan;
import com.net.shop.car.manager.api.model.TuikuanDingdetail;
import com.net.shop.car.manager.api.volley.VolleyCenter;
import com.net.shop.car.manager.api.volley.VolleyListenerImpl;
import com.net.shop.car.manager.api.volley.request.dingdan.GetDingdanOrderDetail;
import com.net.shop.car.manager.api.volley.request.dingdan.GetDingdanOrderList;
import com.net.shop.car.manager.api.volley.request.dingdan.GetOilCardDingDanList;
import com.net.shop.car.manager.api.volley.request.dingdan.GetTuikuanDingdanDetail;
import com.net.shop.car.manager.api.volley.request.dingdan.GetTuikunDingDanList;
import com.net.shop.car.manager.api.volley.response.dingdan.DingdanDetailResponse;
import com.net.shop.car.manager.api.volley.response.dingdan.DingdanOilCardListResponse;
import com.net.shop.car.manager.api.volley.response.dingdan.DingdanOrderListResponse;
import com.net.shop.car.manager.api.volley.response.dingdan.DingdanTuikuanDetailResponse;
import com.net.shop.car.manager.api.volley.response.dingdan.DingdanTuikunListResponse;
import com.net.shop.car.manager.ui.view.AnimatedExpandableListView;
import com.net.shop.car.manager.ui.view.AnimatedExpandableListView.AnimatedExpandableListAdapter;
import com.net.shop.car.manager.utils.Constants.DingDanType;

public class ExpandListAdapter extends AnimatedExpandableListAdapter implements
		OnGroupExpandListener, OnGroupClickListener {

	private List<Dingdan> dingdans = new ArrayList<Dingdan>();

	private Map<Dingdan, DingdanDetail> details = new HashMap<Dingdan, DingdanDetail>();

	private int totalPages;

	private int pageIndex;

	private DingDanType postType;

	private int currentExpandPosition = -1;

	private AnimatedExpandableListView animatedExpandableListView;
	
	private Member user;

	/**
	 * 
	 */
	private BaseActivity activity;

	public ExpandListAdapter(BaseActivity activity,Member user,
			AnimatedExpandableListView animatedExpandableListView,
			DingDanType postType) {
		this.activity = activity;
		this.user = user;
		this.postType = postType;
		this.animatedExpandableListView = animatedExpandableListView;
		pageIndex = 1;
		getData();
	}

	private void getData() {
		activity.progressDialog.show();
		switch (postType) {
		case finish:
			VolleyCenter.getVolley().addGetRequest(
					new GetDingdanOrderList(user.getMemberId(), 2,
							pageIndex++),
					new VolleyListenerImpl<DingdanOrderListResponse>(
							new DingdanOrderListResponse()) {

						@Override
						public void deleveryResponse(
								DingdanOrderListResponse response) {
							activity.progressDialog.dismiss();
							if (response.isSuccess()) {
								totalPages = response.getTotalPages();
								dingdans.addAll(response.getDingdans());
								notifyDataSetChanged();
							}
						}
					});
			break;
		case oilcard:
			VolleyCenter.getVolley().addGetRequest(
					new GetOilCardDingDanList(user.getMemberId(),
							pageIndex++),
					new VolleyListenerImpl<DingdanOilCardListResponse>(
							new DingdanOilCardListResponse()) {

						@Override
						public void deleveryResponse(
								DingdanOilCardListResponse response) {
							activity.progressDialog.dismiss();
							if (response.isSuccess()) {
								totalPages = response.getTotalPages();
								dingdans.addAll(response.getOilCardDingDans());
								notifyDataSetChanged();
							}
						}
					});
			break;
		case drawback:
			VolleyCenter.getVolley().addGetRequest(
					new GetTuikunDingDanList(user.getMemberId(),
							pageIndex++),
					new VolleyListenerImpl<DingdanTuikunListResponse>(
							new DingdanTuikunListResponse()) {

						@Override
						public void deleveryResponse(
								DingdanTuikunListResponse response) {
							activity.progressDialog.dismiss();
							if (response.isSuccess()) {
								totalPages = response.getTotalPages();
								dingdans.addAll(response.getTuiKuanDingdans());
								notifyDataSetChanged();
							}
						}
					});
			break;
		default:
			activity.progressDialog.dismiss();
			break;
		}
	}

	private int count() {
		if (dingdans.size() == 0) {
			return 1;
		} else {
			if (pageIndex <= totalPages) {
				return dingdans.size() + 1;
			}
		}
		return dingdans.size();
	}

	/**
	 * @param txt
	 * @return
	 */
	private TextView getTextView(String txt, ViewGroup viewGroup) {
		TextView textView = (TextView) activity.inflater.inflate(
				R.layout.listitem_txt, viewGroup, false);
		textView.setText(txt);
		textView.setClickable(true);
		return textView;
	}

	@Override
	public int getGroupCount() {
		// TODO Auto-generated method stub
		return count();
	}

	@Override
	public Object getGroup(int groupPosition) {
		// TODO Auto-generated method stub

		return null;
	}

	@Override
	public Object getChild(int groupPosition, int childPosition) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public long getGroupId(int groupPosition) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public long getChildId(int groupPosition, int childPosition) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public boolean hasStableIds() {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public View getGroupView(final int groupPosition, boolean isExpanded,
			View convertView, ViewGroup parent) {
		if (dingdans.size() == 0) {
			return getTextView("暂无数据", parent);// 无数据的情况
		}
		if (pageIndex <= totalPages && groupPosition == count() - 1) {
			TextView textView = getTextView("加载更多", parent);// 当滑动到底部还有更多数据时
			textView.setOnClickListener(new View.OnClickListener() {

				@Override
				public void onClick(View v) {
					getData();
				}
			});
			return textView;
		}
		View currentView = convertView;
		if (currentView == null || currentView instanceof TextView) {
			if (postType == DingDanType.finish) {
				currentView = activity.inflater.inflate(
						R.layout.dingdan_finish_item, parent, false);
				currentView.setTag(new FinishDingdanItem(currentView));
			} else {
				currentView = activity.inflater.inflate(
						R.layout.dingdan_youka_item, parent, false);
				currentView.setTag(new YoukaDingdanItem(activity, currentView));
			}
		}
		final ExpandHolder expandHolder = (ExpandHolder) currentView.getTag();
		expandHolder.setData(dingdans.get(groupPosition), isExpanded);
		return currentView;
	}

	@Override
	public boolean isChildSelectable(int groupPosition, int childPosition) {
		// TODO Auto-generated method stub
		return true;
	}

	@Override
	public View getRealChildView(int groupPosition, int childPosition,
			boolean isLastChild, View convertView, ViewGroup parent) {
		if (postType == DingDanType.finish) {
			return getFinishChildView(groupPosition, childPosition,
					isLastChild, convertView, parent);
		} else if (postType == DingDanType.oilcard
				|| postType == DingDanType.drawback) {
			return getTuikuanChildView(groupPosition, childPosition,
					isLastChild, convertView, parent);
		}
		return getTextView("", parent);
	}

	@Override
	public int getRealChildrenCount(int groupPosition) {
		if (dingdans.size() == 0
				|| (pageIndex <= totalPages && groupPosition == count() - 1)) {
			return 0;
		}
		return 1;
	}

	@Override
	public void onGroupExpand(int groupPosition) {

	}

	@Override
	public boolean onGroupClick(ExpandableListView parent, View v,
			int groupPosition, long id) {
		if (currentExpandPosition == groupPosition) {
			animatedExpandableListView
					.collapseGroupWithAnimation(groupPosition);
			currentExpandPosition = -1;// 没有打开页面
		} else {
			if (currentExpandPosition >= 0) {
				animatedExpandableListView
						.collapseGroupWithAnimation(currentExpandPosition);
			}
			animatedExpandableListView.expandGroupWithAnimation(groupPosition);
			currentExpandPosition = groupPosition;
		}
		notifyDataSetChanged();
		return true;
	}

	/***************************
	 * 已完成订单的页面 *
	 * 
	 ****************************/

	private View getFinishChildView(int groupPosition, int childPosition,
			boolean isLastChild, View convertView, ViewGroup parent) {
		View currentView = convertView;
		if (currentView == null) {
			currentView = activity.inflater.inflate(
					R.layout.dingdan_finish_child_item, parent, false);
			currentView.setTag(new FinishOrderChildItem(currentView));
		}
		final FinishOrderChildItem finishOrderChildItem = (FinishOrderChildItem) currentView
				.getTag();
		final Dingdan dingdan = dingdans.get(groupPosition);
		DingdanDetail detail = details.get(dingdan);
		if (detail == null) {
			finishOrderChildItem.setWaitingState();
			VolleyCenter.getVolley().addGetRequest(
					new GetDingdanOrderDetail(dingdan.getId()),
					new VolleyListenerImpl<DingdanDetailResponse>(
							new DingdanDetailResponse()) {

						@Override
						public void deleveryResponse(
								DingdanDetailResponse response) {
							if (response.isSuccess()) {
								details.put(dingdan, response.getDetail());
								finishOrderChildItem.setFinishDetail(response
										.getDetail());
							}
						}
					});
		} else {
			finishOrderChildItem.setFinishDetail(detail);
		}
		return currentView;
	}

	/**
	 * 已完成订单
	 * 
	 */
	private class FinishOrderChildItem {
		private TextView sellerName;
		private TextView orderId;
		private TextView btn;
		private ProgressBar progressBar;

		public FinishOrderChildItem(View currentView) {
			sellerName = (TextView) currentView
					.findViewById(R.id.dingdan_finish_childitem_uptxt);
			orderId = (TextView) currentView
					.findViewById(R.id.dingdan_finish_childitem_downtxt);
			btn = (TextView) currentView
					.findViewById(R.id.dingdan_finish_childitem_btn);
			progressBar = (ProgressBar) currentView
					.findViewById(R.id.dingdan_finish_childitem_progresss);
		}

		/**
		 * 
		 */
		private void setWaitingState() {
			sellerName.setText("");
			orderId.setText("");
			btn.setClickable(false);
			progressBar.setVisibility(View.VISIBLE);
		}

		/**
		 * @param currentView
		 * @param detail
		 */
		private void setFinishDetail(final DingdanDetail detail) {
			sellerName.setText(detail.getSellerName());
			orderId.setText("订单号: " + detail.getOrderId());
			btn.setOnClickListener(new View.OnClickListener() {

				@Override
				public void onClick(View v) {
					shenSu(detail);
				}
			});
			progressBar.setVisibility(View.GONE);
		}
	}

	private void shenSu(DingdanDetail detail) {

	}

	/***************************
	 * 退款订单的页面 *
	 * 
	 ****************************/
	private View getTuikuanChildView(int groupPosition, int childPosition,
			boolean isLastChild, View convertView, ViewGroup parent) {
		View currentView = convertView;
		if (currentView == null) {
			currentView = activity.inflater.inflate(
					R.layout.dingdan_tuikuan_child_item, parent, false);
			currentView.setTag(new TuiKuanDingdanChildItem(currentView));
		}
		final TuiKuanDingdanChildItem tuiKuanDingdanChildItem = (TuiKuanDingdanChildItem) currentView
				.getTag();
		final Dingdan dingdan = dingdans.get(groupPosition);
		if (dingdan instanceof OilCardDingDan) {
			tuiKuanDingdanChildItem.setOilData((OilCardDingDan) dingdan);
		} else {
			TuikuanDingdetail detail = (TuikuanDingdetail) details.get(dingdan);
			if (detail == null) {
				tuiKuanDingdanChildItem.setWaiting();
				VolleyCenter.getVolley().addGetRequest(
						new GetTuikuanDingdanDetail(dingdan.getId()),
						new VolleyListenerImpl<DingdanTuikuanDetailResponse>(
								new DingdanTuikuanDetailResponse()) {

							@Override
							public void deleveryResponse(
									DingdanTuikuanDetailResponse response) {
								TuikuanDingdetail tuikuanDingdetail = response
										.getTuikuanDingdetail();
								details.put(dingdan, tuikuanDingdetail);
								tuiKuanDingdanChildItem
										.setTuikuanData(tuikuanDingdetail);
							}
						});
			} else {
				tuiKuanDingdanChildItem.setTuikuanData(detail);
			}
		}
		return currentView;
	}

	private class TuiKuanDingdanChildItem {
		private TextView des;
		private TextView btn;
		private ImageView leftIV;
		private ImageView rightIV;
		private TextView leftTxt;
		private TextView leftTime;
		private TextView rightTxt;
		private TextView rightTime;
		private ProgressBar progressBar;

		public TuiKuanDingdanChildItem(View view) {
			des = (TextView) view
					.findViewById(R.id.dingdan_tuikuan_childitem_des);
			btn = (TextView) view
					.findViewById(R.id.dingdan_tuikuan_childitem_btn);
			leftIV = (ImageView) view
					.findViewById(R.id.dingdan_tuikuan_childitem_leftiv);
			rightIV = (ImageView) view
					.findViewById(R.id.dingdan_tuikuan_childitem_rightiv);
			leftTxt = (TextView) view
					.findViewById(R.id.dingdan_tuikuan_childitem_lefttxt1);
			leftTime = (TextView) view
					.findViewById(R.id.dingdan_tuikuan_childitem_lefttxt2);
			rightTxt = (TextView) view
					.findViewById(R.id.dingdan_tuikuan_childitem_righttxt1);
			rightTime = (TextView) view
					.findViewById(R.id.dingdan_tuikuan_childitem_righttxt2);
			progressBar = (ProgressBar) view
					.findViewById(R.id.dingdan_tuikuan_childitem_progress);
		}

		public void setWaiting() {
			des.setText("");
			btn.setText("");
			btn.setOnClickListener(null);
			leftTxt.setText("");
			leftTime.setText("");
			rightTxt.setText("");
			rightTime.setText("");
			progressBar.setVisibility(View.VISIBLE);
		}

		public void setOilData(OilCardDingDan dingDan) {
			progressBar.setVisibility(View.GONE);
			des.setText(dingDan.getGoodName());
			int chongzhiState = dingDan.getChongzhiState();
			leftTime.setText(dingDan.getCreateDate());
			rightTime.setText(dingDan.getCreateDate());
			leftIV.setImageResource(R.drawable.chongzhi);
			if (chongzhiState == 0) {
				btn.setEnabled(false);
				btn.setText("充值中");
				leftTxt.setText("充值中");
				rightTxt.setText("银行待处理");
				rightIV.setImageResource(R.drawable.daichuli_disable);
			} else if (chongzhiState == 1) {
				btn.setEnabled(true);
				btn.setText("充值成功");
				leftTxt.setText("充值中");
				rightTxt.setText("充值成功");
				rightIV.setImageResource(R.drawable.daichuli_enable);
			}

		}

		public void setTuikuanData(TuikuanDingdetail tuikuanDingdetail) {
			progressBar.setVisibility(View.GONE);
			int length = tuikuanDingdetail.getZfb().length();
			des.setText(tuikuanDingdetail.getBankName()+"/尾号" + 
			tuikuanDingdetail.getZfb().substring(length - 5, length -1) +
			"/金额" + tuikuanDingdetail.getRefundMoney().toString() + "元");
			int state = tuikuanDingdetail.getRefundState();
			leftTime.setText(tuikuanDingdetail.getCreateDate());
			rightTime.setText(tuikuanDingdetail.getCreateDate());
			leftIV.setImageResource(R.drawable.shenhzhong);
			switch (state) {
			case 1:
				leftTxt.setText("退款申请成功");
				rightTxt.setText("银行待处理");
				btn.setEnabled(false);
				btn.setText("审核中");
				rightIV.setImageResource(R.drawable.daichuli_disable);
				break;
			case 2:
				leftTxt.setText("退款申请成功");
				rightTxt.setText("银行待处理");
				btn.setEnabled(false);
				btn.setText("审核通过");
				rightIV.setImageResource(R.drawable.daichuli_enable);
				break;
			case 3:
				leftTxt.setText("退款申请成功");
				rightTxt.setText("拒绝退款");
				btn.setEnabled(false);
				btn.setText("退款失败");
				rightIV.setImageResource(R.drawable.daichuli_enable);
				break;
			case 4:
				leftTxt.setText("审核通过");
				rightTxt.setText("退款成功");
				btn.setEnabled(true);
				btn.setText("退款成功");
				rightIV.setImageResource(R.drawable.daichuli_enable);
				break;
			case 5:
				leftTxt.setText("退款申请成功");
				rightTxt.setText("退款失败");
				btn.setEnabled(false);
				btn.setText("退款失败");
				rightIV.setImageResource(R.drawable.daichuli_enable);
				break;
			default:
				break;
			}
		}
	}
}
